package com.qifeng.modules.orderInfo.controller;

import com.qifeng.common.base.RestResponse;
import com.qifeng.modules.orderInfo.domain.OrderInfo;
import com.qifeng.modules.orderInfo.service.OrderInfoService;
import io.swagger.annotations.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "orderInfo")
@RequestMapping("api")
public class OrderInfoController {

	private static Logger logger = Logger.getLogger(OrderInfoController.class);

	@Autowired
	private OrderInfoService orderInfoService;

	/**
	* 获取列表
	*
	* @param response
	* @return
	*/
	@ApiOperation(value = "分页查询所有", notes = "")
	@ApiResponses({
		@ApiResponse(code = 200, message = "查询成功"),
		@ApiResponse(code = 404, message = "查询失败", response = RestResponse.class)
	})
	@GetMapping("orderInfos")
    public RestResponse<Page<OrderInfo>> findPage(Pageable pageable, RestResponse response) {
		return response.success(orderInfoService.findPage(pageable));
	}

	/**
	* 新增
	*
	* @param orderInfo 新增实体，前端数据需要 JSON.stringify(data)
	* @param response
	* @return
	*/
    @ApiOperation(value = "新增", notes = "")
    @ApiResponses({
        @ApiResponse(code = 200, message = "添加成功"),
        @ApiResponse(code = 400, message = "参数验证失败", response = RestResponse.class)
    })
    @PostMapping("orderInfo")
    public RestResponse create(@ApiParam(value = "实体", required = true)
                                 @RequestBody OrderInfo orderInfo,
                               RestResponse response) {
        return response.success(orderInfoService.create(orderInfo));
    }

	/**
	* 更新
	*
	* @param orderInfo 更新实体，前端数据需要 JSON.stringify(data)
	* @param response
	* @return
	*/
    @ApiOperation(value = "修改", notes = "")
    @ApiResponses({
        @ApiResponse(code = 201, message = "修改成功"),
        @ApiResponse(code = 400, message = "参数验证失败", response = RestResponse.class)
    })
    @PatchMapping("orderInfo")
    public RestResponse update(@ApiParam(value = "实体", required = true)
                                 @RequestBody OrderInfo orderInfo,
                               RestResponse response) {
        return response.success(orderInfoService.update(orderInfoService.findOne(orderInfo)));
    }

	/**
	* 删除
	*
	* @param id 主键
	* @param response
	* @return
	*/
    @ApiOperation(value = "删除", notes = "")
    @ApiResponses({
        @ApiResponse(code = 201, message = "修改成功"),
        @ApiResponse(code = 400, message = "参数验证失败", response = RestResponse.class)
    })
    @DeleteMapping("orderInfo")
    public RestResponse delete(@ApiParam(value = "主键", required = true)
                                 @RequestParam Long id,
                               RestResponse response) {
        orderInfoService.delete(orderInfoService.findOne(id));
        return response.success();
    }
    /**
    * 根据ID查找
    *
    * @param id       主键
    * @param response
    * @return
    */
    @ApiOperation(value = "通过主键查找", notes = "")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功"),
        @ApiResponse(code = 404, message = "查询失败", response = RestResponse.class)
    })
    @GetMapping("orderInfo")
    public RestResponse<OrderInfo> findOne(@ApiParam(value = "主键", required = true)
                                  @RequestParam Long id,
                                           RestResponse response) {
        return response.success(orderInfoService.findOne(id));
    }

	/**
	 * 根据postID查找订单下的用户
	 *
	 * @param postId       postID
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "通过职位ID查找当前订单下的用户", notes = "")
	@ApiResponses({
			@ApiResponse(code = 200, message = "查询成功"),
			@ApiResponse(code = 404, message = "查询失败", response = RestResponse.class)
	})
	@GetMapping("orderInfo/{postId}")
	public RestResponse<OrderInfo> findByPostId(@ApiParam(value = "postId", required = true)
										   @PathVariable Long postId,
										   RestResponse response) {
		return response.success(orderInfoService.findByPostId(postId));
	}
}